package com.tosh;

import com.tosh.model.Factor;
import com.tosh.model.Match;
import com.tosh.util.Utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * User: arsentyev
 * Date: 04.10.12
 */
public class Main {
    protected final static Logger _log = Logger.getLogger(Main.class.getName());

    public static void main(String... args) {
        List<Match> matches = new ArrayList<Match>();
        String url = "http://sports.betfair.com/football/competition?id=803237";
        Match match = null;
        Document doc = null;

        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            _log.severe(e.getMessage());
            return;
        }

        Elements trs = doc.select("tbody.vevent");

        for (Element tr : trs) {
//            System.out.println(tr.html());
            match = new Match();

            Elements names = tr.select("td.name a > span");
            match.setMember1(names.get(0).text());
            match.setMember2(names.get(2).text());

            Elements tds = tr.select("td.odds");

            match.addFactor(Factor.WIN1, Utils.tryParseFloat(tds.get(0).text()));
            match.addFactor(Factor.DRAW, Utils.tryParseFloat(tds.get(2).text()));
            match.addFactor(Factor.WIN2, Utils.tryParseFloat(tds.get(4).text()));

            matches.add(match);
        }

        for(Match m: matches) {
            System.out.println("Team 1 = " + m.getMember1());
            System.out.println("Team 2 = " + m.getMember2());
            System.out.println();
            System.out.println("Factors:");
            for(Factor fac: Factor.values()) {
                System.out.printf("%-9s", fac.name());
                System.out.println(" =  " + m.getFactor(fac));
            }
            System.out.println();
            System.out.println();
        }
    }
}